package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.CartService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Cart;
import cn.bytecollege.common.entity.Cuisine;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CartAddController", value = "/admin/cartAdd")
public class CartAddController extends HttpServlet {
    private CartService cartService= null;
    public CartAddController(){
        cartService = new CartService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //1.接收参数
        String id = request.getParameter("cuisineId");
        Integer cuisineId = Integer.valueOf(id);
        //2.封装数据
        Cart cart = new Cart();
        cart.setCuisineId(cuisineId);
        cart.setNumber(1);
        //3.调用方法
         Integer tempNumber = cartService.findByCuisineId(cuisineId);
        if(tempNumber > 0){
            cart.setNumber(tempNumber+1);
            cartService.update(cart);
            response.sendRedirect(request.getContextPath()+"/admin/cuisineList");
            return;
        }
        cartService.save(cart);
        //4.跳转页面
        response.sendRedirect(request.getContextPath()+"/admin/cuisineList");
    }
}
